﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Management.Instrumentation;


namespace littleAdminHelper
{
    class wmi
    {
        private wmi()
        {
        }

        private static ManagementObjectSearcher s = new ManagementObjectSearcher("root\\CIMV2");

        public static String getComputerInformations()
        {
            String output = "";
            Double capacity=0;

            // OS Informations
            s.Query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
            foreach (ManagementObject service in s.Get())
            {
                output += "Operating System: " + service["Caption"].ToString() + "\r\n";
                output += "Architecture: " + service["OSArchitecture"].ToString() + "\r\n";
            }

            // Model Informations
            s.Query = new ObjectQuery("SELECT IdentifyingNumber, Name, Vendor FROM Win32_ComputerSystemProduct");
            foreach (ManagementObject service in s.Get())
            {
                output += "ModellName: " + service["Name"] + "\r\n";
                output += "IdentifyingNumber: " + service["IdentifyingNumber"] + "\r\n";
                output += "Vendor: " + service["Vendor"] + "\r\n";
            }


            // Harddrive Informations
            s.Query = new ObjectQuery("SELECT * FROM Win32_LogicalDisk");
            foreach (ManagementObject service in s.Get())
            {
                if (service["DriveType"].ToString() == "3")
                {
                    output += "Harddisk " + service["Name"].ToString() + " " + service["FreeSpace"].ToString() + "/" + service["Size"].ToString() + "\r\n";
                }
            }



            // Physical Memory Capacity
            s.Query = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
            output +="Memory Capacity: ";
            foreach (ManagementObject service in s.Get())
            {
                capacity += Double.Parse(service["Capacity"].ToString());
            }
            output += capacity + "\r\n";
            return output;
        }

        public static String getComputerName()
        {

            // get Computername
            String output = "";
            s.Query = new ObjectQuery("SELECT Name FROM Win32_ComputerSystem");

            foreach (ManagementObject service in s.Get())
            {
                output += service["Name"].ToString();
            }
            return output;
        }

        public static String getUserName()
        {

            // get UserName
            String output = "";
            s.Query = new ObjectQuery("SELECT FullName FROM Win32_NetworkLoginProfile");

            foreach (ManagementObject service in s.Get())
            {
                output += service["FullName"];
            }
            return output;
        }

        public static String getNetworkConfig()
        {
            String output = "";
            
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_NetworkAdapterConfiguration");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (Boolean.Parse(queryObj["IPEnabled"].ToString()))
                    {
                        output += "Caption: " + queryObj["Description"] + "\r\n";
                        output += "DHCPEnabled: " + queryObj["DHCPEnabled"] + "\r\n";

                        if (queryObj["IPAddress"] == null)
                            output += "IPAddress: " + queryObj["IPAddress"] + "\r\n";
                        else
                        {
                            String[] arrIPAddress = (String[])(queryObj["IPAddress"]);
                            foreach (String arrValue in arrIPAddress)
                            {
                                output += "IPAddress: " + arrValue + "\r\n";
                            }
                        }
                        output += "IPEnabled: " + queryObj["IPEnabled"] + "\r\n";
                        output += "MACAddress: " + queryObj["MACAddress"] + "\r\n";
                        output += "\r\n";
                    }
                }
            return output;
            }

        public static String getCustomValues(String searchdirectory, String searchquery){

            String[] mofValues; 
            String output = "";
            ManagementObjectSearcher s = new ManagementObjectSearcher(searchdirectory, searchquery);

            foreach (ManagementObject queryObj in s.Get())
            {
                // get only the relevand Data
                mofValues = queryObj.GetText(TextFormat.Mof).Split('\"');
                output = mofValues[1];
            }
            return output;
        }

            
            
        }




    }

